Imports System.Data.SqlClient
Imports System.Drawing
Imports DevExpress.XtraGrid
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.XtraGrid.Columns
Imports DevExpress.XtraEditors
Imports DevExpress.XtraScheduler
Imports DevExpress.XtraScheduler.UI
'--- Created Date On : 11-Oct-2010 (Priyabrata)

Public Class frmPatientStatement

    Private mintPatientId As Integer
    Private mstrReportType As String

    Public Sub New(ByVal strRerportType As String)

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        mstrReportType = strRerportType
    End Sub

    Private Sub frmPatientStatement_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            Me.Close()
        End If
    End Sub

    Private Sub frmPatientStatement_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Call PopulateLookUpName()

            dteFromDate.Properties.MaxValue = Today
            dteToDate.Properties.MaxValue = Today
            dteFromDate.EditValue = Format(Today, gstrDtFormat)
            dteToDate.EditValue = Format(Today, gstrDtFormat)
            Me.Text = mstrReportType
            If mstrReportType <> "Patient Statement" Then
                grbDateRange.Visible = False
                Me.Height = 160
                If mstrReportType = "Weight for Age" Then
                    grbPatient.Text = "For Infants( Birth to 36 months)"
                ElseIf mstrReportType = "Weight for Age 2 To 20" Then
                    grbPatient.Text = "For Age 2 To 20 Yrs."
                End If

                glkPatient.Width = 235
                btnSelectPatient.Visible = False
            End If
            glkPatient.Focus()
        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Call AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                 Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub btnViewReport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnViewReport.Click
        Try
            If IsValidEntry() = False Then Exit Sub

            If mstrReportType = "Patient Statement" Then
                Call ShowPatientStatementReport(glkPatient.EditValue, dteFromDate.EditValue, dteToDate.EditValue, chkViewAll.Checked, Me.ParentForm)
            ElseIf mstrReportType = "Length for Age" Then
                Call ShowGrowthReport(glkPatient.EditValue, "tblLengthForAge", "Age", "Height", "", "RptLengthGrowthChart.rpt", "Growth Chart", Me.ParentForm)
            ElseIf mstrReportType = "Weight for Age" Then
                Call ShowGrowthReport(glkPatient.EditValue, "tblWeightForAge", "Age", "Weight", "", "RptWeightGrowthChart.rpt", "Growth Chart", Me.ParentForm)
            ElseIf mstrReportType = "Head Circumference for Age" Then
                Call ShowGrowthReport(glkPatient.EditValue, "tblHeadCirForAge", "Age", "HeadCircumference", "", "RptHeadCirGrowthChart.rpt", "Growth Chart", Me.ParentForm)
            ElseIf mstrReportType = "Weight for Length" Then
                Call ShowGrowthReport(glkPatient.EditValue, "tblWeightForLength", "Length", "Weight", "", "RptWeightLengthChart.rpt", "Growth Chart", Me.ParentForm)
            ElseIf mstrReportType = "BMI For Age" Then
                Call ShowBMIForAgeReport(glkPatient.EditValue, "", "rptBMIForAge.rpt", "BMI For Age", Me.ParentForm)
            ElseIf mstrReportType = "Weight for Age 2 To 20" Then
                Call ShowGrowthReport2To20(glkPatient.EditValue, "tblWeightForAge", "Age", "Weight", "", "RptWeightGrowthChart2To20.rpt", "Growth Chart", Me.ParentForm)
            End If
            Me.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Call AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                 Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub glkPatient_ButtonClick(ByVal sender As Object, ByVal e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles glkPatient.ButtonClick
        Try
            If e.Button.Index = 1 Then
                Call PopulateLookUpName()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Call AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                 Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Function IsValidEntry() As Boolean
        'Here check valid entry of fields
        Dim strMsg As String = ""
        IsValidEntry = False

        If IsDBNullOrNothing(glkPatient.EditValue) Then
            strMsg = "Please select Patient"
            glkPatient.Focus()
        ElseIf mstrReportType = "Patient Statement" AndAlso chkViewAll.Checked = False AndAlso IsDBNullOrNothing(dteFromDate.EditValue) Then
            strMsg = "Please select From Date"
            dteFromDate.Focus()
        ElseIf mstrReportType = "Patient Statement" AndAlso chkViewAll.Checked = False AndAlso IsDBNullOrNothing(dteToDate.EditValue) Then
            strMsg = "Please select To Date"
            dteToDate.Focus()
        End If

        If Trim(strMsg) <> "" Then
            IsValidEntry = False
            MessageBox.Show(strMsg, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        Else
            IsValidEntry = True
        End If
    End Function

    Private Sub PopulateLookUpName()
        Dim dsTemp As DataSet = Nothing
        If mstrReportType = "Patient Statement" Then
            dsTemp = FillDataSet("select PatientId,(IsNull(LastName + ', ' , '') + PatientTitle + ' ' + FirstName + Isnull(' ' + MiddleName, '') ) [Patient Name]" & _
                                  ",Address1 [Address],DateOfBirth [Birth Date] from tblPatientInfo where void=0 ")
        ElseIf mstrReportType = "Weight for Age 2 To 20" Then
            'dsTemp = FillDataSet("SELECT PatientId,IsNull(FirstName,'') + IsNull(' ' + LastName,'')  [Patient Name] FROM tblPatientInfo WHERE DATEDIFF(MM,DateOfBirth,GETDATE()) BETWEEN 0 AND 36")
            dsTemp = FillDataSet("SELECT PatientId,(IsNull(LastName + ', ' , '') + PatientTitle + ' '  + FirstName + Isnull(' ' + MiddleName, '') )  [Patient Name] FROM tblPatientInfo WHERE void=0 and DATEDIFF(YY,DateOfBirth,GETDATE()) BETWEEN 2 AND 20")
        Else
            dsTemp = FillDataSet("SELECT PatientId,(IsNull(LastName + ', ' , '') + PatientTitle + ' '  + FirstName + Isnull(' ' + MiddleName, '') )  [Patient Name] FROM tblPatientInfo WHERE void=0 and DATEDIFF(MM,DateOfBirth,GETDATE()) BETWEEN 0 AND 36")
        End If

        With glkPatient.Properties
            .DataSource = dsTemp.Tables(0)
            .DisplayMember = "Patient Name"
            .ValueMember = "PatientId"
            .NullText = ""
            .ShowFooter = False
            .View.OptionsSelection.EnableAppearanceFocusedCell = False
            .View.OptionsView.AutoCalcPreviewLineCount = True
            .View.OptionsFilter.AllowColumnMRUFilterList = True
            .View.OptionsFilter.AllowFilterEditor = True
            .View.OptionsFilter.AllowMRUFilterList = True
            .View.OptionsView.ShowAutoFilterRow = True
            .View.OptionsView.ShowIndicator = False
            .View.OptionsView.ShowGroupPanel = False
            .View.OptionsView.ShowPreview = True
            '.View.OptionsFilter.ShowAllTableValuesInFilterPopup = True
            '.ImmediatePopup = True
            .ShowFooter = False
            .View.Columns("PatientId").Visible = False
            If mstrReportType = "Patient Statement" Then
                .View.Columns("Birth Date").DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime
                .View.Columns("Birth Date").DisplayFormat.FormatString = "MMM dd,yyyy"
            End If
        End With
    End Sub

    Private Sub btnSelectPatient_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSelectPatient.Click
        Try
            Dim SingleClickOnGridExit As Boolean = True
            Dim SearchPatient As New frmPatientSearch(SingleClickOnGridExit)
            If SearchPatient.ShowDialog() = DialogResult.Cancel Then
                Exit Sub
            End If
            glkPatient.EditValue = SearchPatient.PatientIdForAppointment

        Catch ex As Exception
            DestroyCrushor(Me)
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Call AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                 Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub chkViewAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkViewAll.CheckedChanged
        If chkViewAll.Checked Then
            dteFromDate.Enabled = False
            dteToDate.Enabled = False
            dteFromDate.Visible = False
            dteToDate.Visible = False
            lblFrom.Visible = False
            lblTo.Visible = False
        Else
            dteFromDate.Enabled = True
            dteToDate.Enabled = True
            dteFromDate.Visible = True
            dteToDate.Visible = True
            lblFrom.Visible = True
            lblTo.Visible = True
        End If
    End Sub

    Private Sub glkPatient_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles glkPatient.GotFocus
        'sender.ShowPopup()
    End Sub
End Class
